package com.dcits.selenium;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.dcits.util.DcitsUtil;

/**
 * 自动创建WebDriver实例
 * @author xuwangcheng
 * @version 2018.3.22
 *
 */
public class TestDriver {
	
	public static final String BROWSER_CHROME = "chrome";
	public static final String BROWSER_IE = "ie";
	public static final String BROWSER_FIREFOX = "firefox";
	public static final String BROWSER_OPERA = "opera";
	public static final String BROWSER_PHANTOMJS = "PhantomJS";
	public static final String BROWSER_HTMLUNIT = "HtmlUnit";
	
	private WebDriver driver;
	private String browserName;
	
	/**
	 * 根据浏览器名称实例化不同的driver
	 * @param browserName
	 * @throws MalformedURLException 
	 */
	public TestDriver(String browserName) throws MalformedURLException {
		// TODO Auto-generated constructor stub
		super();		
		this.browserName = browserName;
		if (DcitsUtil.LOCAL_MODE) {//本地模式下使用本地调试
			initWebdriver();
		} else {//非本地模式下使用grid分布式执行
			initRemoteDriver();
		}
	}
	
	/**
	 * 自定义传入driver实例
	 * @param driver
	 */
	public TestDriver(WebDriver driver) {
		super();
		this.driver = driver;
	}
	
	/**
	 * 关闭实例
	 */
	public void closeDriver() {
		if (this.driver != null) {
			driver.quit();
		}
		
	}
	
	/**
	 * 关闭当前标签页窗口
	 */
	public void closeWindow() {
		if (this.driver != null) {
			driver.close();
		}
	}
	
	
	private void initWebdriver() {
		switch (this.browserName) {
		case BROWSER_CHROME:
			this.driver = new ChromeDriver();
			break;
		case BROWSER_IE:
			this.driver = new InternetExplorerDriver();
			break;
		case BROWSER_FIREFOX:
			this.driver = new FirefoxDriver();
			break;
		case BROWSER_OPERA:
			this.driver = new OperaDriver();
			break;
		case BROWSER_PHANTOMJS:
			this.driver = new PhantomJSDriver();
			break;
		case BROWSER_HTMLUNIT:
			this.driver = new HtmlUnitDriver();
			break;
		default:
			this.driver = new ChromeDriver();
			break;
		}
	}
	
	private void initRemoteDriver() throws MalformedURLException {
		DesiredCapabilities capabilities = null;
		switch (this.browserName) {
		case BROWSER_CHROME:
			capabilities = DesiredCapabilities.chrome();
			break;
		case BROWSER_IE:
			capabilities = DesiredCapabilities.internetExplorer();
			break;
		case BROWSER_FIREFOX:
			capabilities = DesiredCapabilities.firefox();
			break;
		case BROWSER_OPERA:
			capabilities = DesiredCapabilities.operaBlink();
			break;
		case BROWSER_PHANTOMJS:
			capabilities = DesiredCapabilities.phantomjs();
			break;
		case BROWSER_HTMLUNIT:
			capabilities = DesiredCapabilities.htmlUnit();
			break;
		default:
			capabilities = DesiredCapabilities.chrome();
			break;
		}
		this.driver = new RemoteWebDriver(new URL(DcitsUtil.HUB_REMOTE_URL), capabilities);
	}
	
	
	public WebDriver getDriver() {		
		return driver;
	}
	
	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}
	
	public void setBrowserName(String browserName) {
		this.browserName = browserName;
	}
	public String getBrowserName() {
		return browserName;
	}
	
}
